package com.example.demo.utils.pay;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayToolsFileUploadRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayToolsFileUploadResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.example.demo.pay.PayNeedData;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Component
public class ZfbPay {
	private PayBean payBean;
	private PayBean.ZFB zfb;

	public ZfbPay(PayBean payBean) {
		this.payBean = payBean;
		this.zfb = payBean.getZfb();
	}

	public String pay(PayNeedData payNeedData) throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient(
				zfb.getGateway_url(),
				zfb.getAppId(),
				zfb.getPrivate_key(),
				"json",
				"utf-8",
				zfb.getPublic_key(),
				"RSA2"
		);

		/** 调取支付宝接口 web端支付*/
		DefaultAlipayClient alipayClients = new DefaultAlipayClient(
				zfb.getGateway_url(),
				zfb.getAppId(),
				zfb.getPrivate_key(),
				"json",
				"utf-8",
				zfb.getPublic_key(),
				"RSA2"
				);


		AlipayTradePagePayRequest request = new AlipayTradePagePayRequest ();
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		request.setNotifyUrl("");
		request.setReturnUrl("");
		JSONObject bizContent = new JSONObject();
		bizContent.put("out_trade_no", "20210817010101004");
		bizContent.put("total_amount", 0.01);
		bizContent.put("subject", "测试商品");
		bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
		request.setBizContent(bizContent.toString());
//bizContent.put("time_expire", "2022-08-01 22:00:00");
//		订单描述
//		model.setBody(payNeedData.getGoodName());
//		订单标题
//		model.setSubject(payNeedData.getGoodName());
//		订单单号
//		model.setOutTradeNo(payNeedData.getOrderId());
//		订单金额
//		model.setTotalAmount(payNeedData.getPrice()+"");
//		request.setBizModel(model);
//		request.setBizContent("{\"out_trade_no\":\""+ payNeedData.getOrderId() +"\","
//				+ "\"total_amount\":\""+ payNeedData.getPrice() +"\","
//				+ "\"subject\":\""+ payNeedData.getGoodName() +"\","
//				+ "\"body\":\"商品名称\","
//				+ "\"timeout_express\":\"90m\","
//				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//		AlipayTradePagePayResponse response =null;
		request.setNotifyUrl("http://v490c76704.yicp.fun/pay/success");
		request.setReturnUrl("http://490767mp04.qicp.vip/#/paySuccess");
		AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
		System.out.println(response.getBody());
		if (response.isSuccess()){
			System.out.println("调用成功");
		}else {
			System.out.println("调用失败");
		}
		return response.getBody().replace('\"','\'').replace('\n',' ');

	}

}
